package org.nvframe.component.render;

import java.util.LinkedHashMap;
import java.util.Map;
import org.nvframe.component.AbstractComponent;
import org.nvframe.entity.Entity;
import org.nvframe.exception.NVFrameException;
import org.nvframe.util.Settings;

/**
 * 
 * @author Nik Van Looy
 */
public class MultipleImageRender extends AbstractComponent {

	private Map<String, ImageRender> renders;

	public MultipleImageRender(String id, Entity owner, Settings settings) throws NVFrameException {
		super(id, owner);
		
		renders = new LinkedHashMap<String, ImageRender>();

		String render = settings.getString("render1", null);
		int renderId = 1;
		
		try {
			// try to search for more renders
			while(render != null) {
				Settings irSettings =  new Settings();
				irSettings.setSetting("image", render);

				String relativeDirection = settings.getString("render"+renderId+"_relativeDirection", "true");
				String zindex = settings.getString("render"+renderId+"_z-index", 
						settings.getString("z-index"));
						
				irSettings.setSetting("relativeDirection", relativeDirection);
				irSettings.setSetting("z-index", zindex);
				
				renders.put(render, new ImageRender(render, owner, irSettings));
				renderId++;
				render = settings.getString("render"+renderId, null);
			}
		} catch (NVFrameException e) {
			throw new NVFrameException("cannot load image renderId:"+renderId+" for MultipleImageRender component");
		}
	}
	
	public void setDirection(String id, float direction) {
		if(renders.containsKey(id))
			renders.get(id).setDirection(direction);
	}
	
	public float getDirection(String id) {
		if(renders.containsKey(id))
			return renders.get(id).getDirection();
		return 0;
	}
	
	@Override
	public void removed(Entity entity) {
		for(AbstractRender render : renders.values())
			render.removed(entity);
	}

}